/******************************************************************
 * Created by: Goh Chee Hong                                    
 * Date of Creation:
 * Date of last modification: 31st/Jan/2009
 * Status: Finished.
 *                                                                
 * Description: a form that show the progess of the uploaded message to openLookUp                         
 * ...
 ******************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using Pbdms.Common.BaseClass;
namespace Pbdms.Forms.Views
{
    public partial class FrmUploadingOfflineMessage : newForms
    {
        Hashtable friends;
        ArrayList attachments;
        BusinessLogic.Mail.Message message;
        Thread thUpload;
        BusinessLogic.Mail.MailUploaderManager publisher;
        private delegate void DelegateWorker(object sender, string args);
        public FrmUploadingOfflineMessage()
        {
            InitializeComponent();
            this.Shown += new EventHandler(FrmUploadingMessage_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmUploadingMessage_FormClosing);
            Color c = new Color();
            c = lblFriends.BackColor;
            Pbdms.Common.Global.paintBackGround(this);
            lblFriends.BackColor = c;
           
            lblUploadFinishedTo.BackColor = c;
        
            textStatus.BackColor = this.BackColor;
        }

        void FrmUploadingMessage_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (thUpload != null)
            {
                if (publisher != null)
                {
                    publisher.Cancel();
                }
                //thUpload.Abort();

            }
        
        }

        /// <summary>
        /// change name
        /// </summary>
        /// <param name="keyID"></param>
        /// <param name="oldName"></param>
        /// <param name="newName"></param>
        public override void changeName(string keyID, string oldName, string newName)
        {
            Boolean found = false;
            foreach (string key in friends.Keys)
            {
                if (key.Equals(keyID))
                {
                    found = true;
                    break;
                }
            }
            if (found == true)
            {
                friends[keyID] = oldName;
                lblFriends.Text = "Friends list\n" + newName;
            }
        }
        void FrmUploadingMessage_Shown(object sender, EventArgs e)
        {
            string friendsList = "";
            foreach(string key in friends.Keys){
                friendsList = friendsList + friends[key]+",";
            }
            friendsList = friendsList.Substring(0, friendsList.Length - 1);
            lblFriends.Text = lblFriends.Text + "\n" + friendsList;
            publisher = new Pbdms.BusinessLogic.Mail.MailUploaderManager();
            publisher.Friends = friends;
            publisher.Attachments = attachments;
            publisher.Message = message;
            publisher.SecKeyControl = Program.keyControl;
            publisher.PublicKeyControl = Program.publicKeyControl;
            publisher.statusProcess += new Pbdms.BusinessLogic.Mail.MailUploaderManager.StatusHandler(StatusUpdate);
            publisher.EachPublishDone += new Pbdms.BusinessLogic.Mail.MailUploaderManager.EachPublishDoneHandler(EachPublishFinished);
            publisher.PublishDone += new Pbdms.BusinessLogic.Mail.MailUploaderManager.PublishDoneHandler(PublishDone);
            publisher.CurrentUser += new Pbdms.BusinessLogic.Mail.MailUploaderManager.CurrentUserHandler(currentUserUpdate);
       
            ThreadStart thsUpload = new ThreadStart(publisher.UploadingMessage);
            thUpload = new Thread(thsUpload);
            thUpload.IsBackground = true;
            thUpload.Start();
        }
        public void setParameters(Hashtable friends, BusinessLogic.Mail.Message message, ArrayList attachments)
        {
            this.friends = friends;
            this.message = message;
            this.attachments = attachments;
        }
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (thUpload != null)
            {
                if (publisher != null)
                {
                    publisher.Cancel();
                }
                thUpload.Abort();
                
            }
            this.Close();
        }
        /// <summary>
        /// a method that keep track the status of the upload
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void StatusUpdate(object sender, string args)
        {
            try
            {
                if (InvokeRequired)
                {
                    DelegateWorker delegateWorker = new DelegateWorker(StatusUpdate);
                    this.BeginInvoke(delegateWorker, new object[] { sender, args });
                }
                else
                {


                    textStatus.Text = args;
                }
            }
            catch
            {

            }
        }
        /// <summary>
        /// a method that update the current user
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void currentUserUpdate(object sender, string args)
        {
            try
            {
                if (InvokeRequired)
                {
                    DelegateWorker delegateWorker = new DelegateWorker(currentUserUpdate);
                    this.BeginInvoke(delegateWorker, new object[] { sender, args });
                }
                else
                {

                    
                }
            }
            catch
            {

            }
        }
        /// <summary>
        /// a method that invoke when the uploading is done
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void EachPublishFinished(object sender, string args)
        {
            try
            {
                if (InvokeRequired)
                {
                    DelegateWorker delegateWorker = new DelegateWorker(EachPublishFinished);
                    this.BeginInvoke(delegateWorker, new object[] { sender, args });
                }
                else
                {
                    //Finished Uploaded To:
                    if (lblUploadFinishedTo.Text.Length != 0)
                    {
                        lblUploadFinishedTo.Text = lblUploadFinishedTo.Text + ",";
                    }
                    lblUploadFinishedTo.Text = lblUploadFinishedTo.Text + args;
                }
            }
            catch
            {

            }
        }
        /// <summary>
        /// a method that updates the status of the button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void PublishDone(object sender, string args)
        {
            try
            {
                if (InvokeRequired)
                {
                    DelegateWorker delegateWorker = new DelegateWorker(PublishDone);
                    this.BeginInvoke(delegateWorker, new object[] { sender, args });
                }
                else
                {
                    btnCancel.Text = "Done";
                }
            }
            catch
            {

            }
        }
        /// <summary>
        /// a method that check whether the uploading is done
        /// </summary>
        /// <returns></returns>
        public Boolean finished()
        {
            if (btnCancel.Text.Equals("Done"))
            {
                return true;
            }
            return false;
        }

    }
}